import requests
import re
import time
import json


def format_map(canable_Date):
    item = canable_Date[0]["unitValue"]
    idxs = 0
    for idx in range(len(canable_Date)):
        if item > canable_Date[idx]["unitValue"]:
            item = canable_Date[idx]["unitValue"]
            idxs = idx
            # val
            # {"timer": item1.x, "unitValue": item1.y}
    return {"unitValue": item, "idx": idxs}


def getTimeStamp():
    # timer = time.time()*1000
    timer = time.time()*1000
    timer1 = int(timer)
    return str(timer1)


def format_data():
    getFunCode = "168203"
    timer = getTimeStamp()
    json_box = requests.get(
        "http://fund.eastmoney.com/pingzhongdata/{0}.js?v={1}".format(getFunCode, timer))
    # print("get cont ")
    json_box = json_box.text
    # print(json_box)
    regext = re.compile(
        "holderStructure\s*\n*\r*=\s*\n*\r*([\s\S\w\W]*?);")
    json_structure = regext.findall(json_box)
    # print("获取规模变动的内容2:")
    # #print(json_structure[0])
    if len(json_structure) >= 1:
        json_structure = json_structure[0]
        # json_structure = json.loads(json_structure)
        json_structure = json.loads(json_structure)
    # 累计净值走势 Data_ACWorthTrend
    # 单位净值走势 Data_netWorthTrend
    regext = re.compile(
        "Data_netWorthTrend\s*\n*\r*=\s*\n*\r*([\s\S\w\W]*?);")
    json_fs_price_list = regext.findall(json_box)
    # 判断单位净值 的 最大回撤部分
    # print("输出单位净值内容:")
    # #print(json_fs_price_list)
    if len(json_fs_price_list) >= 1:
        json_fs_price_listt = json_fs_price_list[0]
        # print("遍历单位净值内容")
        json_fs_price_list = json.loads(json_fs_price_listt)
        # print("获取的单位净值:")
        json_price = []
        for idx in range(len(json_fs_price_list)):
            item1 = json_fs_price_list[idx]
            canable_Date = []
            canable_Date.append(
                {"timer": item1["x"], "unitValue": item1["y"]})
            for idx1 in range(len(json_fs_price_list)):
                if idx1 > idx:
                    item2 = json_fs_price_list[idx1]
                    if item1["y"] <= item2["y"]:
                        canable_Date.append(
                            {"timer": item2["x"], "unitValue": item2["y"]})
                        break
                    else:
                        canable_Date.append(
                            {"timer": item2["x"], "unitValue": item2["y"]})
            boxCont = format_map(canable_Date)
            idxs = boxCont["idx"]
            current_data = canable_Date[idxs]
            y1 = current_data["unitValue"]
            y2 = canable_Date[0]["unitValue"]
            retreat = (y2 - y1)/y2
            # json_price.append(int(retreat))
            j_retreat = float('%.2f' % (float(retreat)*100))
            json_price.append(j_retreat)
        # print("输出遍历得到的 回撤内容")
        j_current = max(json_price)
        # print("最大回撤:")
        j_current = str(j_current)
        print("计算的最大回撤:")
        print(j_current)


if __name__ == '__main__':
    format_data()
